package cn.wetcland.urban.common.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.GifCaptcha;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import cn.wetcland.urban.common.AjaxResponse;
import cn.wetcland.urban.common.MyAssert;
import cn.wetcland.urban.common.component.RedisCommonKey;
import cn.wetcland.urban.common.component.RedisComponent;
import cn.wetcland.urban.common.pojo.InputTipsPOJO;
import cn.wetcland.urban.common.pojo.PathPlanningPOJO;
import cn.wetcland.urban.common.vo.UploadImgVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Api(tags = "公共接口")
@RestController
@RequestMapping("/common")
public class CommonController {

    @Resource
    private RedisComponent redisComponent;

    public static String amapKey = "c4bbac42ed3ee43779ce6b62efca7dbe";

    @ApiOperation("通用-验证码图片")
    @GetMapping("/genVerifyImg/{name}/{w}/{h}")
    public void genLoginVerifyImg(@PathVariable("name") String name, @PathVariable("w") int w, @PathVariable("h") int h, HttpServletRequest request, HttpServletResponse response) throws IOException {
        MyAssert.isTrue(w <= 200 && h <= 200, "验证码图片宽高不能超过200px");
        GifCaptcha gifCaptcha = CaptchaUtil.createGifCaptcha(w, h);
        String code = gifCaptcha.getCode();
        String key = RedisCommonKey.getRedisCommonVerifyCodeKey(name);
        redisComponent.set(key, code);
        // 验证码五分钟过期
        redisComponent.expire(key, 1000 * 60 * 5, TimeUnit.MILLISECONDS);
        gifCaptcha.write(response.getOutputStream());
    }

    @GetMapping("/inputTips")
    public AjaxResponse inputTips(String keywords, Long adcode) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("key", amapKey);
        paramMap.put("keywords", keywords);
        paramMap.put("city", adcode);
        String s = HttpUtil.get("https://restapi.amap.com/v3/assistant/inputtips", paramMap);
        InputTipsPOJO bean = JSONUtil.parseObj(s).toBean(InputTipsPOJO.class);
        MyAssert.isTrue(Objects.equals(bean.getStatus(), "1"), "高德地图API配额不足");
        return AjaxResponse.success(bean.getTips());
    }

    @GetMapping("/pathPlanning")
    public AjaxResponse pathPlanning(BigDecimal originLon, BigDecimal originLat, BigDecimal destinationLon, BigDecimal destinationLat) {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("key", amapKey);
        paramMap.put("origin", originLon.toPlainString()+","+originLat.toPlainString());
        paramMap.put("destination", destinationLon.toPlainString()+","+destinationLat.toPlainString());
        paramMap.put("extensions", "all");
        paramMap.put("output", "json");
        String s = HttpUtil.get("https://restapi.amap.com/v3/direction/driving", paramMap);
        PathPlanningPOJO bean = JSONUtil.parseObj(s).toBean(PathPlanningPOJO.class);
        MyAssert.isTrue(Objects.equals(bean.getStatus(), "1"), "高德地图API配额不足");
        return AjaxResponse.success(bean.getRoute());
    }

}
